﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;
using System.Net;
using System.IO;

namespace App_Code.Helpers
{
    /// <summary>
    /// Summary description for SdarotOrgExtractor
    /// </summary>
    public class SdarotOrgExtractor
    {
        private const string BASE_URL = "http://www.sdarot.org";

        public static List<string> GetMovieUrls(string content)
        {
            var movieUrls = new List<string>();

        

            return movieUrls;
        }

        public static VideoResult ExtractMovieUrl(string movieUrl)
        {        
            var video_id = Regex.Match(movieUrl, "\\/video\\/(.+?)\\/", RegexOptions.Singleline).Groups[1].Value;
            var siteUri = new Uri(BASE_URL);
            var cookie = new Cookie("Private-Video", video_id);

            var pageContent = Utils.GetPage(movieUrl, Encoding.UTF8, siteUri, cookie);
            var video_link = Regex.Match(pageContent, "s1.addVariable.'file', '(.+?)'.;", RegexOptions.Singleline).Groups[1].Value;
            var movieName = Path.GetFileName(movieUrl);
            return new VideoResult(video_link, movieName, true);
        }

        public static bool IsContainVideoLink(string content)
        {
            const string URL_REGEX = ".*?www.sdarot.org.*?";
            var match = Regex.Match(content, URL_REGEX, RegexOptions.IgnoreCase);
            return match.Success;
        }
    }
}